package com.example.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.*;
import com.example.exception.CustomException;
import com.example.mapper.*;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 收藏信息表业务处理
 **/
@Service
public class CollectService {

    @Resource
    private CollectMapper collectMapper;
    @Resource
    private AlbumMapper albumMapper;
    @Resource
    private SingMapper singMapper;
    @Resource
    private SingerMapper singerMapper;
    @Resource
    private PlaylistMapper playlistMapper;
    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 新增
     */
    public void add(Collect collect) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (ObjectUtil.isNull(currentUser)) {
            throw new CustomException(ResultCodeEnum.TOKEN_CHECK_ERROR);
        }
        collect.setUserId(currentUser.getId());
        collect.setTime(DateUtil.now());
        collectMapper.insert(collect);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        collectMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            collectMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Collect collect) {
        collectMapper.updateById(collect);
    }

    /**
     * 根据ID查询
     */
    public Collect selectById(Integer id) {
        return collectMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Collect> selectAll(Collect collect) {
        return collectMapper.selectAll(collect);
    }

    /**
     * 分页查询
     */
    public PageInfo<Collect> selectPage(Collect collect, Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (RoleEnum.USER.name().equals(currentUser.getRole())) {
            collect.setUserId(currentUser.getId());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Collect> list = collectMapper.selectAll(collect);
        for (Collect dbCollect : list) {
            if ("PLAYLIST".equals(dbCollect.getType())) {
                Playlist playlist = playlistMapper.selectById(dbCollect.getRelId());
                if (ObjectUtil.isNotEmpty(playlist)) {
                    dbCollect.setPlaylist(playlist);
                }
            }
            if ("SONG".equals(dbCollect.getType())) {
                Sing sing = singMapper.selectById(dbCollect.getRelId());
                if (ObjectUtil.isNotEmpty(sing)) {
                    Singer singer = singerMapper.selectById(sing.getSingerId());
                    if (ObjectUtil.isNotEmpty(singer)) {
                        sing.setSingerAvatar(singer.getAvatar());
                        sing.setSingerName(singer.getName());
                    }
                    Album album = albumMapper.selectById(sing.getAlbumId());
                    if (ObjectUtil.isNotEmpty(album)) {
                        sing.setAlbumName(album.getName());
                    }
                    Category category = categoryMapper.selectById(sing.getCategoryId());
                    if (ObjectUtil.isNotEmpty(category)) {
                        sing.setCategoryName(category.getName());
                    }
                    dbCollect.setSing(sing);
                }
            }
            if ("ALBUM".equals(dbCollect.getType())) {
                Album album = albumMapper.selectById(dbCollect.getRelId());
                if (ObjectUtil.isNotEmpty(album)) {
                    dbCollect.setAlbum(album);
                }
            }
            if ("SINGER".equals(dbCollect.getType())) {
                Singer singer = singerMapper.selectById(dbCollect.getRelId());
                if (ObjectUtil.isNotEmpty(singer)) {
                    dbCollect.setSinger(singer);
                }
            }
        }
        return PageInfo.of(list);
    }

}